package edu.usm.roberge.client;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;

import edu.usm.roberge.server.HtmlChangedException;

public class PricesAsyncProvider extends AsyncDataProvider<String> {

	boolean loading;
	private GolfCourseServiceAsync golfCourseService;
	
	
	public PricesAsyncProvider(GolfCourseServiceAsync golfCourseService) {
		this.golfCourseService = golfCourseService;
	}


	@Override
	protected void onRangeChanged(HasData<String> display) {
		if (!loading) {
			updateRowCount(0, true);
		}
	}


	public void getAirlinePrices(String value) {
		loading = true;
		
		for(HasData<String> display : getDataDisplays()) {
			display.setVisibleRangeAndClearData(new Range(0, 100), true);
		}
		try {
			golfCourseService.getAirplinePrices(value, null, new AsyncCallback<List<String>>() {

				@Override
				public void onFailure(Throwable caught) {
					
				}

				@Override
				public void onSuccess(List<String> result) {
					loading = false;
					updateRowData(0, result);
				}
				
			});
		} catch (HtmlChangedException e) {
			e.printStackTrace();
		}
	}

}
